;;; pur-mode.el --- Major mode for editing Purify language files -*- lexical-binding: t -*-

;; Author: [Erxat Juma]
;; Version: 0.2
;; Keywords: languages, purify
;; URL: [if applicable]

;;; Commentary:

;; This package provides a major mode for editing Purify language files (.pur).
;; Features:
;; - Syntax highlighting for keywords, constants, functions, strings, numbers, operators, and comments.
;; - Proper handling of identifiers with underscores (e.g., `my_var` won’t highlight `var`).
;; - Derived from `prog-mode` for standard programming mode features.

;;; Code:

(require 'font-lock)

;; 定义 Purify 语言的语法高亮规则
(defconst pur-font-lock-keywords
  (let* ((keywords '("import" "mut"
                     "for" "break" "continue" "match" "in"
                     "if" "else" "is" "orelse" "as"
                     "return"
                     "struct" "impl" "protocol" "pub"
                     "error" "catch"))
         (constants '("null" "void" "self" "true" "false"))
         (operators '("+" "-" "*" "/" "=" "==" "!=" "<" ">" "<=" ">="))
         ;; 匹配独立的关键字，确保前面是空白或括号等非词字符
         (keyword-regexp (concat "\\(?:^\\|[^[:word:]_]\\)\\("
                                 (regexp-opt keywords 'words)
                                 "\\)\\(?:$\\|[^[:word:]_]\\)"))
         (constant-regexp (regexp-opt constants 'words))
         (operator-regexp (regexp-opt operators)))
    `((,keyword-regexp 1 font-lock-keyword-face)              ;; 关键字
      (,constant-regexp . font-lock-constant-face)            ;; 常量
      ("\\<\\([[:alpha:]][[:word:]]*\\)\\s-*::" 1 font-lock-function-name-face) ;; 函数名
      ("\\<\\([[:alpha:]][[:word:]]*\\)\\s-*(" 1 font-lock-function-name-face) ;; 函数名
      ("\\<\\([[:alpha:]][[:word:]]*\\)\\s-*<" 1 font-lock-function-name-face) ;; 函数名
      ("\\(\\<[[:alpha:]_][[:alnum:]_]*\\)\\s-*:\\s-*" 1 font-lock-variable-name-face)
      ("\"[^\"]*\"" . font-lock-string-face)                  ;; 字符串
      ("\\<[0-9]+\\(?:\\.[0-9]+\\)?\\>" . font-lock-constant-face) ;; 数字
      (,operator-regexp . font-lock-builtin-face)             ;; 运算符
      ("//.*$" . font-lock-comment-face))))                   ;; 注释

;; 定义 Purify 模式的语法表
(defvar pur-mode-syntax-table
  (let ((st (make-syntax-table prog-mode-syntax-table)))
    ;; 下划线作为词组成字符，避免变量名被拆分
    (modify-syntax-entry ?_ "w" st)
    ;; 设置 // 为注释起始符
    (modify-syntax-entry ?/ ". 12" st)
    (modify-syntax-entry ?\n ">" st)
    st)
  "Syntax table for `pur-mode'.")

;;;###autoload
(define-derived-mode pur-mode prog-mode "Pur"
  "Major mode for editing Purify language files."
  :syntax-table pur-mode-syntax-table
  ;; 设置高亮规则
  (setq font-lock-defaults '(pur-font-lock-keywords
                             nil nil nil nil))  ;; (keywords disable-syntactic case-fold syntax-begin extra-args)
  ;; 单行注释以 // 开头
  (setq-local comment-start "// ")
  (setq-local comment-start-skip "//+\\s-*"))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.pur\\'" . pur-mode))

(provide 'pur-mode)

;;; pur-mode.el ends here
